﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace SecureCookie.Implementation.Helper
{
    /// <summary>Helper methods for the <see cref="HttpCookieCollection"/> class.</summary>
    public static class HttpCookieCollectionHelper
    {
        /// <summary>
        /// Safe way for finding cookies, please read http://blogs.infosupport.com/blogs/timm/archive/2008/10/29/ASP.NET-HttpCookie-_2D00_-be-aware.aspx
        /// for explaination why this is needed.
        /// </summary>
        /// <param name="collection">The cookie collection to search in</param>
        /// <param name="name">The name of the cookie to find</param>
        /// <returns>Null if no cookie is found, otherwise the found cookie</returns>
        public static HttpCookie Find(this HttpCookieCollection collection, string name)
        {
            for (int i = 0; i < collection.Count; i++)
            {
                if (string.Equals(collection[i].Name, name, StringComparison.Ordinal))
                {
                    return collection[i];
                }
            }
            return null;
        }
    }
}
